﻿module Supernova {

    export class Spawner {

        public name: string = '';
        public sourceSystem: StarSystem = null;
        public targetSystem: StarSystem = null;
        public shipsLeft: number = 0;
        public delay: number = 0;
        public minDelay: number = 0;
        public maxDelay: number = 0;
        public speed: number = 0;
        public attackPower: number = 0;
        public attackRadius: number = 0;
        public attackDelay: number = 0;
        public armor: number = 0;
        public shields: number = 0;
        public pods: number = 0;

        constructor(name: string, sourceSystem: StarSystem, targetSystem: StarSystem, shipCount: number, minDelay: number, maxDelay: number) {
            this.name = name;
            this.sourceSystem = sourceSystem;
            this.targetSystem = targetSystem;
            this.shipsLeft = shipCount;
            this.minDelay = minDelay;
            this.maxDelay = maxDelay;
        }

        public process(secondsElapsed: number) {
            if (this.shipsLeft > 0) {
                this.delay -= secondsElapsed;
                if (this.delay <= 0) {
                    this.shipsLeft -= 1;
                    this.delay += randomFloat(this.minDelay, this.maxDelay);
                    var sourceBodies = findSystemBodies(this.sourceSystem, function (b) { return b.population > 0; });
                    var targetBodies = findSystemBodies(this.targetSystem, function (b) { return b.population > 0; });
                    if (sourceBodies.length > 0 && targetBodies.length > 0) {
                        var sourceBody = sourceBodies[randomInt(0, sourceBodies.length - 1)];
                        var targetBody = targetBodies[randomInt(0, targetBodies.length - 1)];
                        var ship = new Ship();
                        ship.visible = false;
                        ship.owner = OwnerType.computer;
                        ship.name = this.name;
                        ship.currentLocation = new Location();
                        ship.currentLocation.setXY(sourceBody.x, sourceBody.y);
                        ship.currentOrder = ShipOrder.createWithBody(ShipOrderType.move, targetBody);
                        ship.maxSpeed = this.speed;
                        ship.speed = this.speed;
                        ship.attack = this.attackPower;
                        ship.attackRadius = this.attackRadius;
                        ship.maxAttackWait = this.attackDelay;
                        ship.maxArmor = this.armor;
                        ship.armor = this.armor;
                        ship.maxShields = this.shields;
                        ship.shields = this.shields;
                        ship.bombard = 1;
                        ship.artilleryPods = this.pods;
                        ship.maxBombardWait = SHIP_BOMBARD_DELAY_COMPUTER;
                        ship.code = channel.nextCode++;
                        galaxy.ships.push(ship);
                    } else {
                        this.shipsLeft = 0;
                    }
                }
            }
            if (this.shipsLeft <= 0) {
                var index = galaxy.ai.spawners.indexOf(this);
                galaxy.ai.spawners.splice(index, 1);
            }
        }

    }

}